package com.ulic.customerManApplication.usermodule.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;

import com.ulic.customerManApplication.base.BaseDao;
import com.ulic.customerManApplication.constant.CONSTANT;
import com.ulic.customerManApplication.pager.PagerModel;
import com.ulic.customerManApplication.usermodule.bo.Role;
import com.ulic.customerManApplication.usermodule.bo.User;
import com.ulic.customerManApplication.usermodule.dao.UserDao;
@Component("userDao")
public class UserDaoImpl extends BaseDao implements UserDao {

	private Session session = null;
	@SuppressWarnings("unchecked")
	public User findUserByPwd(String username, String password) {
		session = getHibernateTemplate().getSessionFactory().openSession();
		Criteria cri = session.createCriteria(User.class);
		List list = cri.add(Restrictions.eq("user_Name", username)).add(Restrictions.eq("password", password)).list();
		User user = null;
		if(list!=null && list.size()==1){
			user = (User)list.get(0);
		}
		return user;
	}
	
	public PagerModel getPager(int curPage) {
		String hql = "from User u order by u.um_user_id asc";
		return searchPaginated(hql, curPage, CONSTANT.PageSize);
	}

	/**
	 * Description: 根据id查询用户
	 * @Version1.0 
	 * 2012-7-11 下午05:05:58 李求智 (liqz@huilan.com) created
	 * @param userid
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	@Override
	public User findUserByUserId(String userid) throws Exception {
		session = getHibernateTemplate().getSessionFactory().openSession();
		Criteria cri = session.createCriteria(User.class);
		List<User> list = null;
		try{
			list = cri.add(Restrictions.eq("um_user_id", userid)).list();
		}catch(Exception e){
			e.printStackTrace();
		}
		User user = null;
		if(list.size()!=0){
			user = (User)list.get(0);
		}
		
		return user;
	}

	/**
	 * Description: 修改用户
	 * @Version1.0 
	 * 2012-7-11 下午05:08:57 李求智 (liqz@huilan.com) created
	 * @param user
	 * @throws Exception
	 */
	@Override
	public void updateUser(User user) throws Exception {
		hibernateTemplate.update(user);
	}

	@Override
	public PagerModel getPager(int curPage, User user, String roletype,
			String activietype, String startDate, String endDate,String organOid)throws Exception {
		StringBuffer hql = new StringBuffer("from User u where ");
		String sql = "";
		int count = 0;
		if(user!=null&&user.getUser_Name()!=null&&!"".equals(user.getUser_Name().trim())){
			hql.append("u.user_Name like '%"+user.getUser_Name()+"%' ");
			count ++;
		}
		if(user!=null&&user.getReal_Name()!=null&&!"".equals(user.getReal_Name().trim())){
			if(count>0){
				hql.append("and ");
			}
			hql.append("u.real_Name like '%"+user.getReal_Name()+"%' ");
			count ++;
		}
		if(user!=null&&user.getMobile()!=null&&!"".equals(user.getMobile().trim())){
			if(count>0){
				hql.append("and ");
			}
			hql.append("u.mobile = '"+user.getMobile()+ "' ");
			count ++;
		}
		if(user!=null&&user.getMail()!=null&&!"".equals(user.getMail().trim())){
			if(count>0){
				hql.append("and ");
			}
			hql.append("u.mail = '"+user.getMail()+"' ");
			count ++;
		}
		if(organOid!=null&&!"".equals(organOid)&&organOid.length()>0){
			if(count>0){
				hql.append("and ");
			}
			hql.append("u.organization in( "+organOid+" '0')");
			count ++;
		}
		if(roletype!=null&&!"".equals(roletype.trim())){
			if(count>0){
				hql.append("and ");
			} 
			hql.append("u.um_user_id in (select ur.user from UserRole ur where ur.objectid in (select r.oid from Role r where r.code = '"+roletype.trim()+"')) ");
			count ++;
		}
		if(activietype!=null&&!"".equals(activietype.trim())){
			if(count>0){
				hql.append("and ");
			}
			hql.append("u.um_user_id in (select ua.user from UserActivie ua where ua.objectid in (select a.oid from Activie a where a.code = '"+activietype.trim()+"')) ");
			count ++;
		}
		if(startDate!=null&&!"".equals(startDate.trim())){
			if(count>0){
				hql.append("and ");
			}
			startDate = startDate + " 00:00:00";
			hql.append("u.create_Date >= to_date('"+startDate+"','YYYY-MM-DD hh24:mi:ss') ");
			count ++;
		}
		if(endDate!=null&&!"".equals(endDate.trim())){
			if(count>0){
				hql.append("and ");
			}
			endDate = endDate + " 23:59:59";
			hql.append("u.create_Date <= to_date('"+endDate+"','YYYY-MM-DD hh24:mi:ss') ");
			count ++;
		}
		hql.append(" order by u.um_user_id asc");
		sql = hql.toString();
		if(count == 0){
			sql = sql.replace("where", "");
		}
		return searchPaginated(sql, curPage, CONSTANT.PageSize);
	}

	/**
	 * Description: 获得用户角色
	 * @Version1.0 
	 * 2012-7-17 上午09:33:04 李求智 (liqz@huilan.com) created
	 * @param userid
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	@Override
	public String getUserRoleName(String userid) throws Exception {
		String hql = "from Role r where r.oid in (select ur.objectid from UserRole ur where ur.user = ?)";
		session = hibernateTemplate.getSessionFactory().getCurrentSession();
		Query q = session.createQuery(hql);
		q.setParameter(0, userid);
		List<Role> rList = q.list();
		if(rList.size()>0){
			return rList.get(0).getCode();
		}
		return null;
	}

	@Override
	public String getUserRName(String userid) throws Exception {
		String hql = "from Role r where r.oid in (select ur.objectid from UserRole ur where ur.user = ?)";
		session = hibernateTemplate.getSessionFactory().getCurrentSession();
		Query q = session.createQuery(hql);
		q.setParameter(0, userid);
		List<Role> rList = q.list();
		if(rList.size()>0){
			return rList.get(0).getName();
		}
		return null;
	}
}


